﻿using Dapper;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace ThresholdAssistant.Dapper.Mappers
{
    /// <summary>
    /// Uses the Name value of the <see cref="ColumnAttribute"/> specified to determine
    /// the association between the name of the column in the query results and the member to
    /// which it will be extracted. If no column mapping is present all members are mapped as
    /// usual.
    /// </summary>
    /// <typeparam name="T">The type of the object that this association between the mapper applies to.</typeparam>
    public class ColumnTypeMapper : FallbackTypeMapper
    {
        /// <summary>
        /// 
        /// </summary>
        public ColumnTypeMapper(Type classType) : base(new SqlMapper.ITypeMap[] { new CustomPropertyTypeMap(classType, (type, columnName) =>
        type.GetProperties().FirstOrDefault(prop => prop.GetCustomAttributes(false).OfType<ColumnAttribute>().Any(attr => attr.Name == columnName))), new DefaultTypeMap(classType) })
        {

        }
    }
}